<template>
  <view
    class="foot-btn-groups"
    v-if="
      btnWordsDeal.cancleWord ||
      btnWordsDeal.confirmWord ||
      btnWordsDeal.deleteWord
    "
  >
    <view
      class="cancle-btn"
      v-if="btnWordsDeal.deleteWord"
      @click="handleDeleteBtn"
      >{{ btnWordsDeal.deleteWord }}</view
    >
    <view
      class="cancle-btn"
      v-if="btnWordsDeal.cancleWord"
      @click="handleCancleBtn"
      >{{ btnWordsDeal.cancleWord }}</view
    >
    <view
      class="cancle-btn"
      v-if="order.trade_status == -1 && order.order_type != 7"
      @click="gotoFriendPay"
    >
      好友代付</view
    >
    <view
      class="confirm-btn"
      v-if="order.pay_type == 2 && order.is_pay == 0 && order.trade_status == 2"
      @click="handlePayBtn"
    >
      {{ $t("order.toPay") }}</view
    >
    <template v-if="order.is_can_confirm">
      <view
        class="confirm-btn"
        v-if="
          btnWordsDeal.confirmWord &&
          btnWordsDeal.confirmWord != $t('order.warn')
        "
        @click="handleConfirmBtn"
        >{{ btnWordsDeal.confirmWord }}</view
      >
    </template>
    <view
      class="confirm-btn"
      v-if="
        order.invoice &&
        order.invoice.invoice_pics &&
        order.invoice.invoice_pics.length
      "
      @click="previewImg"
      >查看发票</view
    >
  </view>
</template>

<script>
export default {
  data() {
    return {};
  },
  props: {
    order: {
      type: Object,
      default: () => {
        return {};
      }
    },
    showRefund: {
      type: Boolean,
      default: false
    }
  },
  //   订单状态，-1:默认，0:待发货，1:已取消，2:已发货，3:已确认
  computed: {
    btnWordsDeal() {
      let cancleWord = "",
        confirmWord = "",
        deleteWord = "";
      if (this.order.trade_status == -1) {
        cancleWord = this.$t("order.cancleOrder");
        confirmWord = this.$t("order.toPay");
      }
      if (this.order.trade_status == 0) {
        if (this.order.is_pay == 1) {
          if (this.order.apply_delete == 1) {
            cancleWord =
              this.order.trade_status == 1
                ? this.$t("shop.cancled")
                : this.$t("shop.applying");
          } else {
            cancleWord = this.$t("order.cancleOrder");
            confirmWord = this.$t("order.warn");
          }
        } else {
          cancleWord = this.$t("order.cancleOrder");
          confirmWord = this.$t("order.warn");
        }
      }
      if (this.order.trade_status == 1) {
        deleteWord = this.$t("order.del");
        confirmWord = "";
      }
      if (this.order.trade_status == 2) {
        confirmWord = this.$t("shop.confirm");

        if (this.showRefund && !this.order.apply_delete) {
          cancleWord = this.$t("title.orderRefundList");
        }else{
          cancleWord = "";
        }
      }
      if (this.order.trade_status == 3) {
        deleteWord = this.$t("order.del");
        if (this.showRefund) {
          cancleWord = this.$t("title.orderRefundList");
        } else {
          cancleWord = "";
        }
        confirmWord = this.$t("order.buyAgain");
      }

      // 虚拟商品无再次购买，售后
      if (
        this.order.order_type == 4 ||
        this.order.order_type == 3 ||
        this.order.order_type == 7
      ) {
        if (confirmWord == this.$t("order.buyAgain")) confirmWord = "";
        if (cancleWord == this.$t("title.orderRefundList")) cancleWord = "";
      }

      return { cancleWord, confirmWord, deleteWord };
    }
  },
  methods: {
    gotoFriendPay() {
      this.$utils.toUrl(
        `/order/pay-another/pay-another?orderId=${this.order.order_id}`
      );
      return;
    },
    handleCancleBtn() {
      if (this.btnWordsDeal.cancleWord == this.$t("shop.applying")) return;
      this.$emit("handleCancleBtn", {
        id: this.order.order_id,
        status: this.order.trade_status,
        is_delivery: this.order.is_delivery,
        cancleWord: this.btnWordsDeal.cancleWord
      });
    },
    handleConfirmBtn() {
      this.$emit("handleConfirmBtn", {
        id: this.order.order_id,
        status: this.order.trade_status,
        confirmWord: this.btnWordsDeal.confirmWord
      });
    },
    // 处理货到付款订单支付
    handlePayBtn() {
      this.$emit("handleConfirmBtn", {
        id: this.order.order_id,
        status: -1, // 货到付款的线上支付
        confirmWord: this.btnWordsDeal.confirmWord
      });
    },
    handleDeleteBtn() {
      this.$emit("handleDeleteBtn", {
        id: this.order.order_id
      });
    },
    previewImg() {
      let invoice_pics = this.order.invoice.invoice_pics;
      if (invoice_pics && typeof invoice_pics == "string") {
        invoice_pics = JSON.parse(invoice_pics);
        uni.previewImage({
          current: "0",
          urls: invoice_pics.map((el) => el.pic_url)
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.foot-btn-groups {
  width: 100%;
  height: calc(99rpx + env(safe-area-inset-bottom));
  background: #ffffff;
  border: 2rpx solid #f0f0f0;
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 12;
  padding-right: 35rpx;
  display: flex;
  justify-content: flex-end;
  padding-top: 20rpx;

  view {
    width: 152rpx;
    height: 60rpx;
    background: #ffffff;
    border-radius: 30rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24rpx;
    font-weight: 500;
  }

  .cancle-btn {
    color: #262626;
    margin-right: 24rpx;
    border: 1rpx solid #8e8e8e;
  }

  .confirm-btn {
    background: linear-gradient(90deg, #f2140c, #f14c0c);
    color: #ffffff;
  }
}
</style>
